package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.PO.CloudIntercomAccountInfoPO;
import com.itlong.cloud.POJO.PO.CloudIntercomAppidInfoPO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateTokenImportDao;
import com.itlong.cloud.operate.service.IOperateTokenImportService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      导入第三方提供账号token服务接口实现类
 * </desc>
 *
 * @createDate 2018/07/16
 */
@Service
public class OperateTokenImportServiceImpl implements IOperateTokenImportService {

    @Autowired
    private IOperateTokenImportDao iOperateTokenImportDao;

    /**
     * <desc>
     *      第三方提供云对讲token账号导入
     * </desc>
     *
     * @param accountAndToken 账号和token数据集
     * @return 受影响的行数
     * @author wangzhi
     * @createDate 2018/07/16
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer importCloudIntercomToken(Map<String, String> accountAndToken) throws Exception {
        Date nowTime = new Date();
        List<CloudIntercomAccountInfoPO> cloudIntercomAccountInfoPOS = new ArrayList<>();
        // 遍历账号和token信息的map
        for (Map.Entry<String, String> entry : accountAndToken.entrySet()){
            CloudIntercomAccountInfoPO cloudIntercomAccountInfoPO = new CloudIntercomAccountInfoPO();
            cloudIntercomAccountInfoPO.setCloudIntercomAccountId(LogicIdUtil.bussinessId());
            cloudIntercomAccountInfoPO.setAccountType(2);
            cloudIntercomAccountInfoPO.setAccount(entry.getKey());
            cloudIntercomAccountInfoPO.setToken(entry.getValue());
            cloudIntercomAccountInfoPO.setBindingStatus(0);
            cloudIntercomAccountInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
            cloudIntercomAccountInfoPO.setCreateTime(nowTime);
            cloudIntercomAccountInfoPO.setUpdateTime(nowTime);
            cloudIntercomAccountInfoPOS.add(cloudIntercomAccountInfoPO);
        }
        return iOperateTokenImportDao.batchSave(cloudIntercomAccountInfoPOS);
    }

    /**
     * <desc>
     *      第三方提供云对讲appid应用账号导入
     * </desc>
     *
     * @param appid appid数据集
     * @return 受影响的行数
     * @author Juguang.S
     * @createDate 2018/12/19
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer importCloudIntercomAppid(Map<String, String> appid) throws Exception {
        Date nowTime = new Date();
        ArrayList<Map<String, Object>> rowList = new ArrayList<>();
        Map<String, Object> map;
        // 遍历账号和token信息的map
        for (Map.Entry<String, String> entry : appid.entrySet()){
            CloudIntercomAppidInfoPO cloudIntercomAppidInfoPO = new CloudIntercomAppidInfoPO();
            cloudIntercomAppidInfoPO.setCloudIntercomAppidId(LogicIdUtil.bussinessId());
            cloudIntercomAppidInfoPO.setAppid(entry.getKey());
            cloudIntercomAppidInfoPO.setBindingStatus(0);
            cloudIntercomAppidInfoPO.setIsJiaRAppid(0);
            cloudIntercomAppidInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
            cloudIntercomAppidInfoPO.setCreateTime(nowTime);
            cloudIntercomAppidInfoPO.setUpdateTime(nowTime);
            map = SqlUtil.durableData(cloudIntercomAppidInfoPO, PlatformConstants.TABLE_SAVE);
            rowList.add(map);
        }
        return iOperateTokenImportDao.batchSaveAppid(rowList);
    }

}
